using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class NewMainMenu_Hypothetical_ContextMenu : MonoBehaviour
{
	public List<TextMeshProUGUI> BackgroundTextLines = new List<TextMeshProUGUI>();

	[SerializeField]
	private List<int> BackgroundTextMaxCount = new List<int>();

	[SerializeField]
	private NewMainMenu_HypotheticalSubMenu hypotheticalSubMenu;

	[SerializeField]
	private TextMeshProUGUI optionText;

	private Coroutine currentMainTextWriting;

	private int CurrentSelected;

	private int previousSelected = -1;

	public Hypothesis CurrentHypothesis;

	[SerializeField]
	private NewMainMenu_Hypothetical_Shortcut HypotheticalShortcutMenu;

	[SerializeField]
	private TextMeshProUGUI HypotheticalTitle;

	[SerializeField]
	private TextMeshProUGUI HypotheticalDescription;

	[SerializeField]
	private TextMeshProUGUI Warning;

	[SerializeField]
	private TextMeshProUGUI HypotheticalQuests;

	[SerializeField]
	private Image HypotheticalIcon;

	private bool AllTextShown;

	private bool wasEnabled;

	private bool AllowInput = true;

	[SerializeField]
	private Image HypotheticalDisclaimer_BlackBackground;

	[SerializeField]
	private TextMeshProUGUI HypotheticalDisclaimer_Text;

	private void Awake()
	{
		CurrentSelected = PlayerPrefs.GetInt("NewMainMenu_PreviousSelectedHypothetical", 0);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			BackgroundTextMaxCount.Add(backgroundTextLine.text.Length);
			backgroundTextLine.maxVisibleCharacters = 0;
		}
	}

	private void Update()
	{
		if (AllowInput)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Right))
			{
				CurrentSelected++;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Left))
			{
				CurrentSelected--;
				CheckCurrentSelectedOutsideBounds();
				CheckUpdateScreenText();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) || Input.GetKeyDown(PlayerInput.Instance.Key_Menu))
			{
				SelectOption();
			}
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Cancel))
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
				hypotheticalSubMenu.CheckUpdateScreenText(GreenHighlighted: false);
				hypotheticalSubMenu.AllowInput = true;
				base.gameObject.SetActive(value: false);
			}
		}
		if (!wasEnabled && base.gameObject.activeSelf)
		{
			previousSelected = -1;
			wasEnabled = true;
			AllowInput = true;
			CurrentSelected = 0;
			StartCoroutine(ShowBackgroundText());
			CheckUpdateScreenText();
			SetupHypothetical();
		}
	}

	private void SelectOption()
	{
		PlayerPrefs.SetInt("NewMainMenu_PreviousSelectedHypothetical", CurrentSelected);
		AllowInput = false;
		previousSelected = -1;
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuSelect);
		StartCoroutine(SelectOptionTimer());
		CheckUpdateScreenText();
	}

	private IEnumerator SelectOptionTimer()
	{
		yield return new WaitForSeconds(0.5f);
		if (CurrentHypothesis != null && CurrentHypothesis.HypotheticalShortcuts.Length != 0 && PlayerPrefs.GetInt("TimesPlayed_" + CurrentHypothesis.HypothesisName, 0) > 0)
		{
			if (CurrentSelected == 0)
			{
				GonerMenu.CurrentActiveHypothesis = CurrentHypothesis;
				StartCoroutine(HypotheticalDisclaimer(CurrentHypothesis));
				DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.CheekyGrin);
			}
			else if (CurrentSelected == 1)
			{
				StartCoroutine(SelectShortcutTimer());
				CheckUpdateScreenText();
				DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.Default);
			}
			else
			{
				NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
				hypotheticalSubMenu.CheckUpdateScreenText(GreenHighlighted: true);
				hypotheticalSubMenu.AllowInput = true;
				base.gameObject.SetActive(value: false);
			}
		}
		else if (CurrentSelected == 0)
		{
			GonerMenu.CurrentActiveHypothesis = CurrentHypothesis;
			StartCoroutine(HypotheticalDisclaimer(CurrentHypothesis));
			DEBUG_EnableMarkiplier.ChangeMarkiplierState(DEBUG_EnableMarkiplier.MarkiplierEmotions.CheekyGrin);
		}
		else
		{
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuDeny);
			hypotheticalSubMenu.CheckUpdateScreenText(GreenHighlighted: true);
			hypotheticalSubMenu.AllowInput = true;
			base.gameObject.SetActive(value: false);
		}
	}

	public IEnumerator HypotheticalDisclaimer(Hypothesis hypothetical, HypothesisShortcut Shortcut = null)
	{
		CutsceneUtils.FadeInUIImage(HypotheticalDisclaimer_BlackBackground, 2f);
		yield return new WaitForSeconds(0.5f);
		float remainingConfirmPresses = 3f;
		CutsceneUtils.FadeInText(HypotheticalDisclaimer_Text, 2f);
		CutsceneUtils.PlaySound(NewMainMenuManager.instance.SFX_MenuNotification, CutsceneUtils.DRH_MixerChannels.Effect, 0.7f);
		HypotheticalDisclaimer_Text.text = hypothetical.HypotheticalDisclaimer + $"请再按 <color=yellow>{remainingConfirmPresses}</color> 次 <color=yellow>{PlayerInput.Instance.Key_Confirm}</color> 键继续。";
		while (remainingConfirmPresses > 0f)
		{
			if (Input.GetKeyDown(PlayerInput.Instance.Key_Confirm) && remainingConfirmPresses > 0f)
			{
				CutsceneUtils.PlaySound(NewMainMenuManager.instance.SFX_MenuMove, CutsceneUtils.DRH_MixerChannels.Effect, 0.5f);
				remainingConfirmPresses -= 1f;
				HypotheticalDisclaimer_Text.text = hypothetical.HypotheticalDisclaimer + $"请再按 <color=yellow>{remainingConfirmPresses}</color> 次 <color=yellow>{PlayerInput.Instance.Key_Confirm}</color> 键继续。";
			}
			else
			{
				HypotheticalDisclaimer_Text.text = hypothetical.HypotheticalDisclaimer + $"请再按 <color=yellow>{remainingConfirmPresses}</color> 次 <color=yellow>{PlayerInput.Instance.Key_Confirm}</color> 键继续。";
			}
			yield return null;
		}
		CutsceneUtils.PlaySound(NewMainMenuManager.instance.SFX_MenuSelect, CutsceneUtils.DRH_MixerChannels.Effect, 0.5f);
		UI_LoadingIcon.ToggleLoadingIcon(showIcon: true);
		CutsceneUtils.FadeInText(HypotheticalDisclaimer_Text);
		yield return new WaitForSeconds(0.5f);
		if ((bool)Shortcut)
		{
			UI_FADE.Instance.StartFadeIn(Shortcut.ShortcutSceneDestination, 1f);
		}
		else
		{
			UI_FADE.Instance.StartFadeIn(hypothetical.HypothesisStartingScene, 1f);
		}
	}

	private IEnumerator SelectShortcutTimer()
	{
		yield return new WaitForSeconds(0.5f);
		GonerMenu.CurrentActiveHypothesis = CurrentHypothesis;
		HypotheticalShortcutMenu.CurrentHypothesis = CurrentHypothesis;
		HypotheticalShortcutMenu.gameObject.SetActive(value: true);
		base.gameObject.SetActive(value: false);
	}

	private void CheckUpdateScreenText()
	{
		if (previousSelected != CurrentSelected)
		{
			previousSelected = CurrentSelected;
			string text = "";
			string[] array = new string[2] { "游玩 ", "返回" };
			optionText.fontSize = 72f;
			if (CurrentHypothesis != null && CurrentHypothesis.HypotheticalShortcuts.Length != 0 && PlayerPrefs.GetInt("TimesPlayed_" + CurrentHypothesis.HypothesisName, 0) > 0)
			{
				// WS3917 - Note: Shortcuts translation is unsure.
				array = new string[3] { "游玩 ", "跳转 ", "返回" };
				optionText.fontSize = 48f;
			}
			for (int i = 0; i < array.Length; i++)
			{
				text = ((CurrentSelected != i) ? (text + array[i]) : (AllowInput ? (text + "<color=yellow>-> " + array[i] + "</color>") : (text + "<color=green>-> " + array[i] + "</color>")));
			}
			ChangeMainText(text, AllowAnimation: true);
		}
	}

	private void CheckCurrentSelectedOutsideBounds()
	{
		if (CurrentHypothesis != null && CurrentHypothesis.HypotheticalShortcuts.Length != 0 && PlayerPrefs.GetInt("TimesPlayed_" + CurrentHypothesis.HypothesisName, 0) > 0)
		{
			if (CurrentSelected < 0)
			{
				CurrentSelected = 2;
			}
			if (CurrentSelected > 2)
			{
				CurrentSelected = 0;
			}
		}
		else
		{
			if (CurrentSelected < 0)
			{
				CurrentSelected = 1;
			}
			if (CurrentSelected > 1)
			{
				CurrentSelected = 0;
			}
		}
		NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuMove);
	}

	private void SetupHypothetical()
	{
		HypotheticalTitle.text = CurrentHypothesis.HypothesisName;
		HypotheticalDescription.text = "<color=grey>猜想是这样的...\n\n</color>" + CurrentHypothesis.HypothesisDescription;
		if (CurrentHypothesis.HypothesisMenuSprite != null && CurrentHypothesis.HypotheticalReleased)
		{
			HypotheticalIcon.sprite = CurrentHypothesis.HypothesisMenuSprite;
		}
		else
		{
			HypotheticalIcon.sprite = hypotheticalSubMenu.noIconSprite;
		}
		Warning.text = CurrentHypothesis.TimeWhenWritten;
		string text = "";
		text = ((CurrentHypothesis.HypothesisGoals.Length == 0) ? (text + "<color=red>【本猜想没有小任务】</color>") : (text + "<color=green>【可选有奖小任务】</color>"));
		HypothesisGoal[] hypothesisGoals = CurrentHypothesis.HypothesisGoals;
		foreach (HypothesisGoal hypothesisGoal in hypothesisGoals)
		{
			int num = PlayerPrefs.GetInt("HypothesisGoal_" + hypothesisGoal.GoalPlayprefName, 0);
			text += "\n";
			HypotheticalQuests.color = Color.white;
			if (hypothesisGoal.RequiredTasks > 1)
			{
				text = ((num >= hypothesisGoal.RequiredTasks) ? (text + "<color=yellow>") : (text + "<color=white>"));
				text = text + "(" + num + "/" + hypothesisGoal.RequiredTasks + ")";
			}
			else
			{
				text = ((num == 0) ? (text + "<color=white>(未完成)") : (text + "<color=yellow>(已完成)"));\()
			}
			text += hypothesisGoal.GoalHint;
		}
		HypotheticalQuests.text = text;
	}

	private void OnDisable()
	{
		wasEnabled = false;
		AllTextShown = false;
		HypotheticalIcon.sprite = hypotheticalSubMenu.noIconSprite;
		ChangeMainText("", AllowAnimation: false);
		foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
		{
			backgroundTextLine.maxVisibleCharacters = 0;
		}
	}

	private IEnumerator ShowBackgroundText()
	{
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX"))
		{
			List<TextMeshProUGUI> finishedTexts = new List<TextMeshProUGUI>();
			while (!AllTextShown)
			{
				yield return null;
				foreach (TextMeshProUGUI backgroundTextLine in BackgroundTextLines)
				{
					if (!finishedTexts.Contains(backgroundTextLine) && backgroundTextLine.maxVisibleCharacters >= backgroundTextLine.text.Length)
					{
						finishedTexts.Add(backgroundTextLine);
					}
					else if (backgroundTextLine.maxVisibleCharacters < backgroundTextLine.text.Length)
					{
						float num = 100 + Random.Range(-60, -10);
						backgroundTextLine.maxVisibleCharacters += Mathf.CeilToInt(num * Time.deltaTime);
						backgroundTextLine.maxVisibleCharacters = Mathf.Clamp(backgroundTextLine.maxVisibleCharacters, 0, backgroundTextLine.text.Length);
						NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
					}
				}
				if (CompareTextLists(finishedTexts, BackgroundTextLines))
				{
					AllTextShown = true;
					MonoBehaviour.print("all text shown");
				}
			}
			yield break;
		}
		foreach (TextMeshProUGUI backgroundTextLine2 in BackgroundTextLines)
		{
			backgroundTextLine2.maxVisibleCharacters = backgroundTextLine2.text.Length;
		}
	}

	private bool CompareTextLists(List<TextMeshProUGUI> list1, List<TextMeshProUGUI> list2)
	{
		if (list1.Count != list2.Count)
		{
			return false;
		}
		for (int i = 0; i < list1.Count; i++)
		{
			if (list1[i].text != list2[i].text)
			{
				return false;
			}
		}
		return true;
	}

	public void ChangeMainText(string text, bool AllowAnimation)
	{
		optionText.text = text;
		if (!SettingsManager.Instance.GetBoolSettingValue("SimpleVFX") && AllowAnimation)
		{
			optionText.maxVisibleCharacters = 0;
			if (currentMainTextWriting != null)
			{
				StopCoroutine(currentMainTextWriting);
			}
			currentMainTextWriting = StartCoroutine(ScrollMainText());
		}
		else
		{
			optionText.maxVisibleCharacters = text.Length;
		}
	}

	private IEnumerator ScrollMainText()
	{
		yield return null;
		while (optionText.maxVisibleCharacters < optionText.text.Length)
		{
			float num = 600 + Random.Range(-120, 0);
			optionText.maxVisibleCharacters += Mathf.CeilToInt(num * Time.deltaTime);
			optionText.maxVisibleCharacters = Mathf.Clamp(optionText.maxVisibleCharacters, 0, optionText.text.Length);
			NewMainMenuManager.instance.MenuSource.PlayOneShot(NewMainMenuManager.instance.SFX_MenuClick);
			yield return null;
		}
	}
}
